<template>
  <el-main class="subject">
    <div>
      <!-- Form -->
      <el-button
        type="primary"
        @click="dialogFormVisible = !dialogFormVisible"
        round
        class="primaryButton"
        style="margin-right: 10px;width:150px"
         v-if="getRoleInSession"
        
        >添加报修记录
      </el-button>

      <el-dialog
        title="添加报修记录"
        :visible.sync="dialogFormVisible"
        
        width="500px"
      >
        <el-form :model="maintenanceForm">
          <el-form-item label="楼房号" :label-width="formLabelWidth">
             <el-select v-model="maintenanceForm.housId" placeholder="请选择楼房号" filterable clearable>
              <el-option
                v-for="item in housIdList"
                :key="item.housId"
                :value="item.housId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="宿舍号" :label-width="formLabelWidth">
             <el-select v-model="maintenanceForm.dormId" placeholder="请选择宿舍号" filterable clearable>
              <el-option
                v-for="item in dormIdList"
                :key="item.dormId"
                :value="item.dormId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="报修人" :label-width="formLabelWidth">
            <el-input
              v-model="maintenanceForm.maintName"
              autocomplete="on"
              class="inputSize"
            ></el-input>
          </el-form-item>
           <el-form-item label="报修物品" :label-width="formLabelWidth">
            <el-input
              v-model="maintenanceForm.maintDes"
              autocomplete="on"
              class="inputSize"
              
            ></el-input>
          </el-form-item>
          <el-form-item label="报修时间" :label-width="formLabelWidth">
          <el-date-picker
              v-model="maintenanceForm.maintTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间"
              :picker-options="pickerOptions0"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="维修时间" :label-width="formLabelWidth">
           <el-date-picker
              v-model="maintenanceForm.repDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间"
              :picker-options="pickerOptions0"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addMaintenance()">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="修改报修记录"
        :visible.sync="dialogFormVisibleByUpdate"
        width="500px"
      >
        <el-form :model="maintenanceForm">
          <el-form-item label="报修号" :label-width="formLabelWidth">
            <el-input
              v-model="maintenanceForm.mainId"
              autocomplete="on"
              class="inputSize"
              disabled
            ></el-input>
          </el-form-item>
           <el-form-item label="楼房号" :label-width="formLabelWidth">
             <el-select v-model="maintenanceForm.housId" placeholder="请选择楼房号" filterable clearable>
              <el-option
                v-for="item in housIdList"
                :key="item.housId"
                :value="item.housId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="宿舍号" :label-width="formLabelWidth">
             <el-select v-model="maintenanceForm.dormId" placeholder="请选择宿舍号" filterable clearable>
              <el-option
                v-for="item in dormIdList"
                :key="item.dormId"
                :value="item.dormId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="报修人" :label-width="formLabelWidth">
            <el-input
              v-model="maintenanceForm.maintName"
              autocomplete="on"
              class="inputSize"
              
            ></el-input>
          </el-form-item>
           <el-form-item label="报修物品" :label-width="formLabelWidth">
            <el-input
              v-model="maintenanceForm.maintDes"
              autocomplete="on"
              class="inputSize"
              
            ></el-input>
          </el-form-item>
          <el-form-item label="报修时间" :label-width="formLabelWidth">
          <el-date-picker
              v-model="maintenanceForm.maintTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间"
              :picker-options="pickerOptions0"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="维修时间" :label-width="formLabelWidth">
           <el-date-picker
              v-model="maintenanceForm.repDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期时间"
              :picker-options="pickerOptions0"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateMaintenance()">确 定</el-button>
        </div>
      </el-dialog>

      <el-button
        type="primary"
        round
        class="primaryButton"
        style="width: 150px"
        @click="getAllMaintenance()"
        >查询全部报修记录</el-button
      >

      <el-input
        placeholder="
         楼房号
        "
        v-model="findKeyByHId"
        style="width: 120px; margin-left: 40%"
        clearable
      >
      </el-input>
      <el-input
        placeholder="
         宿舍号
        "
        v-model="findKeyByDId"
        style="width: 120px; margin-left: 1%"
        clearable
      >
      </el-input>
      <el-button
        icon="el-icon-search"
        circle
        type="primary"
        style="margin-left: 10px"
        @click="getMaintenanceById()"
      ></el-button>
    </div>

    <div style="margin-top: 30px; width:1200px">
      <el-table
        :data="maintenanceTableData"
        border
        :style="styleByRole"
        v-loading="loading"
        tooltip-effect="dark"
        @selection-change="selectionChange($event)"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column fixed prop="mainId" label="报修号" width="150">
        </el-table-column>
        <el-table-column prop="housId" label="楼房号" width="120">
        </el-table-column>
        <el-table-column prop="dormId" label="宿舍号" width="120">
        </el-table-column>
        <el-table-column prop="maintName" label="报修人" width="120">
        </el-table-column>
        <el-table-column prop="maintTime" label="报修时间" width="140">
         
        </el-table-column>
        <el-table-column prop="repDate" label="维修时间" width="140">
        </el-table-column>
        <el-table-column prop="maintDes" label="报修物品" width="120">
        </el-table-column>
        
        <el-table-column fixed="right" label="操作" width="160" v-if="getRoleInSession">
          <template slot-scope="scope">
            <el-button
              @click="dialogFormVisibleShow(scope.row)"
              type="primary"
              circle
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              circle
              size="small"
              @click="deleteMaintenance(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 10px; margin-left: 20%">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="page.pageSizes"
        :page-size="page.sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.pageTotal"
      >
      </el-pagination>
    </div>
  </el-main>
</template>

<script>
import { Loading } from "element-ui";
export default {
  name: "Maintenance",
  data() {
    return {
      maintenanceTableData: [],
      dormIdList:[],
      housIdList:[],
      //宿舍号和楼房号
      findKeyByHId: "",
      findKeyByDId:"",
      dialogFormVisible: false,
      dialogFormVisibleByUpdate: false,
      loading: true,
      multipleSelection: [],
      pickerOptions0:{
       disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;//如果没有后面的-8.64e6就是不可以选择今天的
          }
      },
      eqMethod: {
        methodNum: 0,
      },
      // maintenance表单
      maintenanceForm: {
        mainId: "",
        housId: "",
        dormId: "",
        maintName: "",
        maintTime: "",
        repDate: "",
        maintDes:""
      },
      formLabelWidth: "130px",
      //传输报修记录数据的默认参数
      maintenance: {
        mainId: "0",
        housId: "0",
        dormId: "0"
      },
      //传输page数据的默认参数
      page: {
        currentPage: 1,
        sizePage: 5,
        pageTotal: 0,
        pageSizes: [3, 5, 8],
      },
      // 传输数据的容器
      objectData: {
        maintenance: {},
        page: {},
      },
    };
  },
  computed:{
    getRoleInSession(){
      return sessionStorage.getItem("rid")==="1"?true:false
    },
    styleByRole(){
       return sessionStorage.getItem("rid")==="1"?"width:1000px":"width:800px"
    }
  },
  methods: {
    handleClick(a) {
      console.log(a);
    },
    //选择一次展现几个时候触发
    handleSizeChange(size) {
      console.log("handleSizeChange变换");
      this.page.sizePage = size;
      // this.getAllMaintenance();
      if (this.eqMethod.methodNum === 1) {
        this.getAllMaintenance();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getMaintenanceById();
      }
      if (this.eqMethod.methodNum === 3) {
        this.getMaintenanceByName();
      }
      console.log("this.page.sizePage" + this.page.sizePage);
    },
    //当前页变化时候触发
    handleCurrentChange(currentPageNum) {
      this.page.currentPage = currentPageNum;
      if (this.eqMethod.methodNum === 1) {
        this.getAllMaintenance();
      }
      if (this.eqMethod.methodNum === 2) {
        this.getMaintenanceById();
      }
      if (this.eqMethod.methodNum === 3) {
        this.getMaintenanceByName();
      }
      // console.log(this.eqMethod.methodNum===1);
      // this.getAllMaintenance();
    },
    // 获得全部报修记录
    getAllMaintenance() {
      this.eqMethod.methodNum = 1;
      console.log(
        this.$globalConfig.testUrl + "maintenance/getAllMaintenance/" + this.objectData
      );
      this.objectData.maintenance = this.maintenance;
      this.objectData.page = this.page;
      console.log(this.objectData);
      this.$axios
        .post(this.$globalConfig.testUrl + "maintenance/getAllMaintenance/", {
          page: this.page,
        })
        .then(
          (response) => {
            console.log("请求成功了", response.data);
            this.maintenanceTableData = response.data.extend.maintenanceList;
            this.page.currentPage4 = response.data.extend.currentPage;
            this.page.sizePage = response.data.extend.sizePage;
            this.page.pageTotal = response.data.extend.pageTotal;
            this.loading = false;
          },
          (error) => {
            console.log("请求失败了", error.message);
            this.loading = false;
          }
        );
    },
    //按照宿舍楼房号来查询
    getMaintenanceById() {
      this.eqMethod.methodNum = 2;
      this.maintenance.housId = this.findKeyByHId ;
      this.maintenance.dormId = this.findKeyByDId;
      console.log({
        object: this.maintenance,
        page: this.page,
      });
      if (this.maintenance.dormId != "" && this.maintenance.housId!="") {
        this.$axios
          .post(this.$globalConfig.testUrl + "maintenance/getOneMaintenance", {
            object: this.maintenance,
            page: this.page,
          })
          .then(
            (response) => {
              console.log("请求成功了", response.data);
              this.maintenanceTableData = response.data.extend.maintenanceList;
              this.page.currentPage4 = response.data.extend.currentPage;
              this.page.sizePage = response.data.extend.sizePage;
              this.page.pageTotal = response.data.extend.pageTotal;
              this.loading = false;
            },
            (error) => {
              console.log("请求失败了", error.message);
              this.loading = false;
            }
          );
      } else {
        alert("您输入的报修号是空");
      }
    },
    //添加报修记录
    addMaintenance() {
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "maintenance/addMaintenance",
        data: this.maintenanceForm,
      }).then(
        (response) => {
          console.log("请求成功了getMaintenanceByName", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllMaintenance();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      
      this.dialogFormVisible = false;
    },
    //删除报修记录
    deleteMaintenance(main) {
      // console.log(stu.stuId);
      // this.maintenance.stuId = stu.stuId;
      // 弹窗提示框
      this.$confirm("此操作将永久删除该条信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            url: this.$globalConfig.testUrl + "maintenance/deleteMaintenance",
            data: { dormId: main.dormId,housId:main.housId,mainId:main.mainId },
          }).then(
            (response) => {
              if (response.data.msg === "处理成功")
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              console.log("请求成功了deleteMaintenance", response.data);
              this.getAllMaintenance();
            },
            (error) => {
              console.log("请求失败了", error.message);
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });

      // 将数据恢复原样
      this.maintenance.stuId = "";
    },
    // 展示对话框
    dialogFormVisibleShow(main) {
      this.maintenanceForm = main;
      this.dialogFormVisibleByUpdate = true;
    },
    // 修改报修记录
    updateMaintenance() {
      console.log(this.maintenanceForm);
      // this.maintenanceForm.maintTime+=1;
      // this.maintenanceForm.repDate+=1;
      this.$axios({
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        url: this.$globalConfig.testUrl + "maintenance/updateMaintenance",
        data: this.maintenanceForm,
      }).then(
        (response) => {
          // console.log("请求成功了updateMaintenance", response.data);
           if(response.data.code===200){
              this.$message({
                message: '处理成功',
                type: 'success'
               });
          }else{
              this.$message.error('处理失败');
          }
          this.getAllMaintenance();
        },
        (error) => {
          console.log("请求失败了", error.message);
        }
      );
      this.dialogFormVisibleByUpdate = false;
    },
     getAllDromHId(){
          this.$axios
            .post(this.$globalConfig.testUrl + "dorm/getAllDromHId",{"dormId":this.maintenanceForm.dormId})
            .then(
            (response) => {
                console.log("getAllDHIdList 请求成功了", response.data);
                this.housIdList = response.data.extend.HIdList;
            },
            (error) => {
                console.log("请求失败了", error.message);
            }
        );
    },
    getAllDromDId(){
      this.$axios
          .post(this.$globalConfig.testUrl + "dorm/getAllDromDId",{"housId":this.maintenanceForm.housId})
          .then(
          (response) => {
              console.log("getAllDHIdList 请求成功了", response.data);
              this.dormIdList = response.data.extend.DIdList;
          },
          (error) => {
              console.log("请求失败了", error.message);
          }
        );
    },
    // 这里可以做多个删除
    selectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
      this.multipleSelection.forEach((a)=>{
        console.log(a.stuId);
      })
    },
  },
  watch:{
      maintenanceForm:{
					deep:true,
					handler(){
            this.getAllDromHId();
						this.getAllDromDId();
					}
				}
  },
  mounted() {
    this.getAllMaintenance();
    this.getAllDromDId();
    this.getAllDromHId();
    // console.log("Maintenance被挂载了")
    // this.$bus.$emit('ControlHomeSpan',false);
  },
  beforeDestroy() {
    // console.log("Main被摧毁了")
  },
};
</script>

<style scoped>
.subject {
  position: absolute;
  top: 100px;
  left: 40px;
  flex: auto;
  background-color: white;
}
.primaryButton {
  width: 100px;
  height: 40px;
  font-size: 16px;
}
.inputSize {
  width: 200px;
}
</style>